<?php

/**
 * @category   Ac
 * @package    Application_Model
 * @copyright  Orient Network Ltd
 * @license    Closed
 * @author     Olivier Bregeras
 */
class Model_Testimonial extends Ac_Model
{

    const STATUS_ACTIVE  = 1;
    const STATUS_DELETED = 3;

    protected static  $_collection = 'testimonial';

    /**
     *
     * @var array define the requirements for this model
     */
    protected static $_requirements = array(
        //'un'  => array('Required', 'Validator:EmailAddress'), //Email
    );

    protected static $_transcript = array(
        '_id'	=> '_id',
        'nm'   	=> 'name',
        'nm_tc'	=> 'name_tc',
        'tl'   	=> 'tagline',
        'tl_tc'	=> 'tagline_tc',
        'me'   	=> 'message',
        'me_tc'	=> 'message_tc',
        'lo'   	=> 'logo',
        'cn'   	=> 'company_name',
        'c_dt' 	=> 'created_at',
        'us'   	=> 'user_id',
        'nv'   	=> 'number_views',
   );

    public function __construct($data = array(), $config = array())
	{
        $config['collection'] = 'testimonial';
        self::ensureIndex(array('c_dt'));
        parent::__construct($data, $config);
    }

    public function preSave()
    {
        if (empty($this->c_dt)) {
            $this->c_dt = new MongoDate(time());
        }
        if (!is_object($this->c_dt)) {
            $this->c_dt = new MongoDate($this->c_dt);
        }
        if (empty($this->st)) {
            $this->st = self::STATUS_ACTIVE;
        } 
    }

    /**
     * We use Map/Reduce do to calculation on the number of views
     * @return int
     */
    public function getNoViews()
    {
        return $this->number_views;
    }

    public function __get($name)
    {
        if ($name == 'user') {
            $id = parent::__get('user');
            $res = Model_User::find($this->us);
            return $res;
        } elseif($name == 'created_at') {
            $get = parent::__get('c_dt');
            if (!empty($get)) {
                if (is_object($get)) {
                    return $get->sec;
                } else {
                    return strtotime($get);
                }
            }            
        } else {
            return parent::__get($name);
        }
    }
    
    public static function getHomepage()
    {
        $count = self::all()->count();
        $num = rand(0, $count-1);
        
        $res = self::all()->skip($num)->limit(1);
        $res->rewind();
        return $res->current();
    }
    
    public static function getSignuppage()
    {
        $test = self::all(array('st' => 1));
        $arr = array();
        foreach($test as $k => $testi){
			$arr[] = $testi;
		}
//		echo "<pre>";
//        print_r($arr);
//        echo "</pre>";
        
        shuffle($arr);
        $arr = array_slice($arr, 0, 2);
        
        return $arr;
    }
}